/**
 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
 *
 * This file is part of Dicoogle/dicoogle.
 *
 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * QueryHistory.java
 *
 * Created on 21/Jul/2010, 0:04:27
 */

package pt.ua.dicoogle.rGUI.client.windows;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.Semaphore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import pt.ua.dicoogle.Main;
import pt.ua.dicoogle.core.QueryHistoryEntry;
import pt.ua.dicoogle.core.QueryHistorySupport;

/**
 *
 * @author samuelcampos
 */
@Deprecated
public class QueryHistory extends javax.swing.JFrame implements Observer {
    private QueryHistorySupport QHS = QueryHistorySupport.getInstance();
    private JTextField queryField = null;
    private JCheckBox jCheckBoxKeywords = null;

    private static QueryHistory instance = null;
    private static Semaphore sem = new Semaphore(1, true);

    public static synchronized QueryHistory getInstance()
    {
        try
        {
            sem.acquire();
            if (instance == null)
            {
                instance = new QueryHistory();
            }
            sem.release();
        }
        catch (InterruptedException ex)
        {
            LoggerFactory.getLogger(QueryHistory.class).error(ex.getMessage(), ex);
        }
        return instance;
    }

    /** Creates new form QueryHistory */
    private QueryHistory() {
        initComponents();

        Image image = Toolkit.getDefaultToolkit().getImage(Thread.currentThread().getContextClassLoader().getResource("trayicon.gif"));
        this.setIconImage(image);

        QHS.addObserver(this);
        
        updateList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListQueryHistory = new javax.swing.JList();
        jButtonClose = new javax.swing.JButton();
        jButtonInsert = new javax.swing.JButton();
        jButtonDeleteThis = new javax.swing.JButton();
        jButtonDeleteAll = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Query History");
        setMinimumSize(new java.awt.Dimension(490, 270));

        jListQueryHistory.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListQueryHistory.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListQueryHistoryMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListQueryHistory);

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jButtonInsert.setText("Insert into Query");
        jButtonInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInsertActionPerformed(evt);
            }
        });

        jButtonDeleteThis.setText("Delete Query");
        jButtonDeleteThis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteThisActionPerformed(evt);
            }
        });

        jButtonDeleteAll.setText("Delete All");
        jButtonDeleteAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteAllActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel2.setText("Query History:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(jButtonDeleteAll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(jButtonDeleteThis, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(jButtonInsert, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(jButtonClose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(jButtonInsert)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonDeleteThis)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonDeleteAll)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 84, Short.MAX_VALUE)
                        .add(jButtonClose))
                    .add(layout.createSequentialGroup()
                        .add(1, 1, 1)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        this.setVisible(false);
        MainWindow.getInstance().toFront();
        this.dispose();
    }//GEN-LAST:event_jButtonCloseActionPerformed

    private void jButtonInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInsertActionPerformed
        QueryHistoryEntry<String, Boolean> query = (QueryHistoryEntry<String, Boolean>) jListQueryHistory.getSelectedValue() ;
        if (query==null || query.getKey()==null)
        {

            JOptionPane.showMessageDialog(this, "Select a valid query.", "Missing query", JOptionPane.INFORMATION_MESSAGE);
            return ;
        }
        queryField.setText(query.getKey());
        jCheckBoxKeywords.setSelected(query.getValue());
    }//GEN-LAST:event_jButtonInsertActionPerformed

    private void jButtonDeleteAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteAllActionPerformed
        QHS.deleteAll();
    }//GEN-LAST:event_jButtonDeleteAllActionPerformed

    private void jButtonDeleteThisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteThisActionPerformed
        QueryHistoryEntry<String, Boolean> query = (QueryHistoryEntry<String, Boolean>) jListQueryHistory.getSelectedValue() ;
        if(query == null)
            JOptionPane.showMessageDialog(this, "Please select one query to delete.", "Delete Query", JOptionPane.INFORMATION_MESSAGE);
        else
            QHS.deleteQuery(query);
    }//GEN-LAST:event_jButtonDeleteThisActionPerformed

    private void jListQueryHistoryMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListQueryHistoryMouseClicked
        if (evt.getClickCount() == 2)
              jButtonInsert.doClick();
    }//GEN-LAST:event_jListQueryHistoryMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonDeleteAll;
    private javax.swing.JButton jButtonDeleteThis;
    private javax.swing.JButton jButtonInsert;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jListQueryHistory;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


     @Override
    public void update(Observable o, Object arg) {
        updateList();
    }

    private void updateList(){
        Iterator<QueryHistoryEntry<String, Boolean>> it = QHS.getQueryHistory();

        DefaultListModel listModel = new DefaultListModel();

        while(it.hasNext())
        {
            listModel.addElement(it.next());
        }
        jListQueryHistory.setModel(listModel);
    }

    public void setJTextFieldQuery(JTextField queryField, JCheckBox jCheckBoxKeywords){
        this.queryField = queryField;
        this.jCheckBoxKeywords = jCheckBoxKeywords;
    }
}
